<template>
  <div class="app-container selectDeptClass">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="6" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入部门名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--设备数据-->
      <el-col :span="18" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="" prop="paramDoubleQuery">
            <el-input
              v-model="queryParams.paramDoubleQuery"
              placeholder="请输入组态名称/设备名称"
              clearable
              prefix-icon="el-icon-search"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            <el-button icon="el-icon-finished" size="mini" type="success" @click="empowerClick">授权</el-button>
          </el-form-item>
        </el-form>
    <el-table v-loading="loading" :data="managerList">
      <el-table-column label="组态名称" align="center" prop="pageName" />
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="操作" align="center" width="260">
        <template slot-scope="scope">
          <span v-if="judgeCurrentUser(scope.row.createBy)">设备创建者</span>
          <el-button
            size="mini"
            type="text"
            v-else
            @click="canceEmpower(scope.row)"
          >取消授权</el-button>
        </template>
      </el-table-column>
    </el-table>  
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
        :page-sizes="[5, 10, 20]"
        :page-size="10"
      />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { listBDeviceZt,empowerDevice} from "@/api/ghxx/bDeviceZt";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import store from '@/store'
export default {
  name: "User",
  dicts: ['sys_normal_disable', 'sys_user_sex'],
  components: { Treeselect },
  props:{
    empowerDevice:Object
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        queryDeptId:null,
        deviceMac: null,
        deviceName: null,
      },
      deptName:'',
      managerList:[],
      deptOptions:[],
      selectDeptName:'',
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getTreeselect();
    this.getConfigKey("sys.user.initPassword").then(response => {
      this.initPassword = response.msg;
    });
  },
  methods: {
    //判断是否当前用户
    judgeCurrentUser(createBy){
      if(store.getters.deptId==this.queryParams.queryDeptId){
        return true;
      }else if(!this.queryParams.queryDeptId){
        return true;
      }else{
        return false;
      }
    },
    canceEmpower(row){
      if(!this.selectDeptName){
          this.msgSuccess("请选择部门");
          return; 
      }
      let that=this;
      this.$confirm('您是否确认取消对'+this.empowerDevice.deviceName+'的授权?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          let data={
            deviceId:row.id,
            deptId:that.queryParams.queryDeptId,
            empowerType:'设备取消授权'
          }
          return empowerDevice(data);
        }).then(function() {
          that.msgSuccess("取消授权成功") 
          that.getList()
        })      
    },
    empowerClick(){
      if(!this.selectDeptName){
          this.msgSuccess("请选择分配部门");
          return; 
      }
      let that=this;
      this.$confirm('您是否确认分配'+this.empowerDevice.pageName+'到' + this.selectDeptName + '么?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          let data={
            deviceId:that.empowerDevice.id,
            deptId:that.queryParams.queryDeptId,
            empowerType:'设备授权'
          }
          return empowerDevice(data);
        }).then(function() {
          that.msgSuccess("授权成功") 
          that.getList()
        })
    },
    /** 查询设备列表 */
    getList() {
      this.loading = true
      listBDeviceZt(this.queryParams).then(response => {
        this.managerList = response.rows
        this.total = response.total
        this.loading = false
      });
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.queryDeptId = data.id+"";
      this.selectDeptName=data.label
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {      
        this.selectData=selection;
    },
    //确定按钮
    selectUserConfirm(){
        return this.selectData;
    }
  }
};
</script>
<style scoped>
.selectDeptClass{
  height: 430px;
  overflow-y: auto;
}
</style>